extern crate rand;

use std::io;
use std::cmp::Ordering;
use rand::Rng;

fn main() {
    // println!("Hello, world!");
    println!("Guess the Number!");

    let secret_num = rand::thread_rng().gen_range(1, 100);
    // 如果想出现提示，把这行放开
    // println!("The secret number is :{}", secret_num);

    loop {
        println!("Please input your guess.");

        let mut guess = String::new();
        io::stdin().read_line(&mut guess).expect("Failed to read line.");

        println!("Your guess is {}", guess);

        // let guess_num:u32 = guess.trim().parse().expect("请输入一个数值");
        let guess_num:u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                // _会匹配所有错误信息
                println!("请输入数值!");
                continue;
            }
        } ;

        // 比较输入与随机值
        match guess_num.cmp(&secret_num) {
            Ordering::Less => println!("Too small!"),
            Ordering::Greater=>println!("Too big!"),
            Ordering::Equal => {
                println!("You win!");
                break;
            }
        }
    }

}
